home *** CD-ROM | disk | FTP | other *** search
/ Programmer Power Tools / Programmer Power Tools.iso / microcrn / issue_46.arc / CING46.ARC / SINES.C < prev   
Encoding:
C/C++ Source or Header  |  1988-05-11  |  1.4 KB  |  74 lines

  1. /*
  2.      From the C'ing Clearly column in Micro Cornucopia Magazine Issue #46
  3.  
  4.      Program:  Sines
  5.  
  6.      Version:  1.00
  7.      Date:     29-Nov-1988
  8.  
  9.      Language: ANSI C
  10.  
  11.      Computes all of the sines of the angles between 0 and 360 degrees.
  12.  
  13.      Developed by Scott Robert Ladd. This program is public domain.
  14. */
  15.  
  16. #define pi2rad 57.29577951
  17.  
  18. /* prototypes */
  19. void main(void);
  20. double fact(double);
  21. double power(double, double);
  22.  
  23. void main()
  24.     {
  25.     double angle, radians, sine, worksine, temp, k;
  26.  
  27.     for (angle = 0.0; angle <= 360.0; angle += 1.0)
  28.         {
  29.         radians = angle / pi2rad;
  30.         k = 0.0;
  31.         worksine = 0.0;
  32.  
  33.         do  {
  34.             sine = worksine;
  35.             temp = (2.0 * k) + 1.0;
  36.             worksine += (power(-1.0,k) / fact(temp)) * power(radians,temp);
  37.             k += 1.0;
  38.             }
  39.         while (sine != worksine);
  40.         }
  41.     }
  42.  
  43. /* Note: this function is designed for speed; it ONLY works when n is integral */
  44. double fact(double n)
  45.     {
  46.     double res;
  47.  
  48.     res = 1.0;
  49.  
  50.     while (n > 0.0)
  51.         {
  52.         res *= n;
  53.         n -= 1.0;
  54.         }
  55.  
  56.     return res;
  57.     }
  58.  
  59. /* Note: this function is designed for speed; it ONLY works when p is integral */
  60. double power(double n, double p)
  61.     {
  62.     double res;
  63.  
  64.     res = 1.0;
  65.  
  66.     while (p > 0.0)
  67.         {
  68.         res *= n;
  69.         p -= 1.0;
  70.         }
  71.  
  72.     return res;
  73.     }
  74.